#include "Boomerang.h"

#include "Resource.h"

Boomerang::Boomerang(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, bool isLeft) : Object(x, y)
{	 
	p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[47]->getFileName()), 125, 0);
	 
	this->pos_x = x;
	this->pos_y = y;

	pos_x_first = x;

	this->veloc_x = BOOMERANG_VX;
	/*this->veloc_y = BOOMERANG_VY;
	this->accel_y = BOOMERANG_GRAVITY;*/

	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();

	if (isLeft == true)
	{
		
		this->isLeft = true;
		this->veloc_x = -BOOMERANG_VX;
		//p_sprite->setFrame(0,4);	//frame an
	} 
	else
	{
		
		this->isLeft = false;
		//p_sprite->setFrame(4,8);	//frame an
		//p_sprite->setIndex(4);
	}

	obj_state = IS_ACTIVING;

}

Boomerang::~Boomerang()
{
	if (p_sprite != NULL)
	{
		delete p_sprite;
	}
}

void Boomerang::render(int cameraX, int cameraY)
{
	if (obj_state != IS_REMOVE)
	{
		p_sprite->render(pos_x, pos_y, cameraX, cameraY);
	}
}

OBJECT_TYPE Boomerang::getObjectType()
{
	return OBJECT_ITEM;
}


void Boomerang::update(list<Object*>* obj_list, DxInput* input, float gameTime)
{
	switch(obj_state)
	{
	case IS_IDLE:
		break;

	case IS_ACTIVING:
		{
			width = p_sprite->getTexture()->getWidth();
			height = p_sprite->getTexture()->getHeight();
			 
			pos_x_old = pos_x;
			pos_y_old = pos_y;

			pos_x += veloc_x * gameTime;
			/*veloc_y -= accel_y * gameTime;
			pos_y += veloc_y * gameTime + 1/2.0 * accel_y * gameTime * gameTime;*/

			Object* obj;
			list<Object*>::iterator i_obj;
			for (i_obj = obj_list->begin(); i_obj != obj_list->end(); i_obj++)
			{
				obj = *i_obj;
					Box box(this->pos_x_old,
						this->pos_y_old - this->height, 
						width, 
						height, 
						veloc_x * gameTime, 
						0);

					Box block(obj->getPosX(),
						obj->getPosY() - obj->getHeight(),
						obj->getWidth(),
						obj->getHeight());

					Box boxphase = Collision::GetSweptBroadphaseBox(box);
					if (Collision::AABBCheck(boxphase, block))
					{
						float normalx = 0;
						float normaly = 0;
						float collision_time = Collision::SweptAABB(box, block, normalx, normaly);
						if (0 < collision_time && collision_time < 1)
						{
						}

						switch (obj->getIDObjType())
						{
						case 11:	//ghost
						case 14:	//red bat
						case 16:	//stone knight
							{
								if (obj->getObjState() == ES_ACTIVING)
								{
									obj->changeState(ES_DIED);
								}
							}
							break;

						case 13:	//bat boss
						case 18:	//head boss
							{
								int blood = obj->getBlood();
								blood--;
								obj->setBlood(blood);
								if (obj->getBlood() == 0)
									obj->changeState(ES_DIED);
								else
									obj->changeState(ES_PAUSE);
							}
							break;
						}

				}//end if
			}//end for

			pos_x_old = pos_x;
			pos_y_old = pos_y;

			if (abs(pos_x - pos_x_first) > 250)
			{
				//changeState(IS_REMOVE);
				veloc_x = -veloc_x;
			}
			if (abs(pos_x - pos_x_first) > 0 && abs(pos_x - pos_x_first) < 1)
			{
				changeState(IS_REMOVE);
			}
			
		}
		break;

	}
	p_sprite->update(gameTime);

}


void Boomerang::changeState(int state)
{
	obj_state = state;
	switch(state)
	{
	case IS_ACTIVING:		
		veloc_x = BOOMERANG_VX;
		break;	 

	case IS_REMOVE:
		break;
	}
}

